const AdminBiz = require('../../../../store/admin_biz_config.js');
const pathResolver = require('../config/path_resolver');
const pathImport = require('../helpers/path_import.js');
const { pageHelper } = pathImport;
// 导入MobX相关依赖
const { createStoreBindings } = require('../../../../../../miniprogram_npm/mobx-miniprogram-bindings/index');
// 导入管理员流程Store
const { adminFlowStore } = require('../../../../store/admin_store.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (!AdminBiz.isAdmin(this)) return;

		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: adminFlowStore,
			fields: [
				'isLoad',
				'dataList',
				'sortItems',
				'sortMenus'
			],
			actions: [
				'setLoading',
				'initSearchMenu',
				'loadFlowList',
				'setFilter',
				'deleteFlow'
			]
		});

		// 初始化搜索菜单
		this.initSearchMenu();
		
		// 设置加载状态
		this.setLoading(true);
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () {
		if (this.isLoad) {
			await this.loadFlowList();
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		// 清理MobX绑定
		this.storeBindings.destroyStoreBindings();
	},

	/**
	 * 路由跳转
	 */
	url: async function (e) {
		pageHelper.url(e, this);
	},

	/**
	 * 列表事件监听
	 */
	bindCommListCmpt: function (e) {
		pageHelper.commListListener(this, e);
	},

	/**
	 * 删除流程
	 */
	bindDelTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');

		let callback = async () => {
			await this.deleteFlow(id);
		}
		pageHelper.showConfirm('确认删除？删除不可恢复', callback);
	}
});